package com.fm.financemanage.controller;





import com.fm.financemanage.component.MinioComponent;
import com.fm.financemanage.component.RedisComponent;
import com.fm.financemanage.constant.ResponseEnum;
import com.fm.financemanage.response.Callback;
import com.fm.financemanage.response.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author ：ycx
 * @version :1.0
 * @date ：Created in 2021/9/2 14:57
 * @description：公用接口
 * @modified By：
 */
@RequestMapping("/common")
@Api(tags = "公用接口")
@RestController
public class CommonController {
    @Autowired
    private MinioComponent minioComponent;

    @Autowired
    private RedisComponent redisComponent;

    /**
     * 文件上传
     * 文件保存位置还未确定
     *
     * @param request
     * @param response
     * @param file
     * @return
     */
    @PostMapping("/file/upload")
    @ApiOperation("图片上传")
    public JsonData multipleUpload(HttpServletRequest request, HttpServletResponse response
            , @ApiParam(name = "file", value = "文件上传")
                                   @RequestParam("file") MultipartFile[] file,
                                   @ApiParam(name = "folder", value = "存储目录", required = true)
                                   @RequestParam(name = "folder", required = true) String folder
    ) {

        try {

            StringBuilder result = new StringBuilder();

            for (int i = 0; i < file.length; i++) {
                MultipartFile multipartFile = file[i];
                String url = minioComponent.upload(multipartFile.getInputStream(), multipartFile.getOriginalFilename(), folder);

//                String replace = url.replace("http:", "https:");
                result.append(url).append(",");
            }
            if (result.toString().endsWith(",")) {
                result.delete(result.length() - 1, result.length());
            }
            return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), result.toString());


        } catch (Exception e) {
            e.printStackTrace();
        }
        return Callback.fail(ResponseEnum.UNEXCEPTION.getCode(), ResponseEnum.UNEXCEPTION.getMemo());
    }


    @GetMapping("/sts/temp")
    @ApiOperation("获取OSS临时key")
    public JsonData makeStsTempInfo(HttpServletRequest request, HttpServletResponse response,
                                    @ApiParam(name = "folder", value = "存储目录", required = true)
                                    @RequestParam(name = "folder", required = false) String folder,
                                    @ApiParam(name = "fileName", value = "文件名称", required = true)
                                    @RequestParam(name = "fileName", required = false) String fileName
    ) {
        String url = minioComponent.getPresignedUploadUrl(folder + "/" + fileName);
        return Callback.fail(ResponseEnum.UNEXCEPTION.getCode(), ResponseEnum.UNEXCEPTION.getMemo());
    }

}